package com.cxyd.modules.wms.controller;

import com.cxyd.common.Utils.ExcelUtils;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.DateUtils;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wms.dto.SpuDTO;
import com.cxyd.modules.wms.dto.StockDTO;
import com.cxyd.modules.wms.excel.SpuExcel;
import com.cxyd.modules.wms.excel.StockExcel;
import com.cxyd.modules.wms.service.SpuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/spu")
public class SpuController {
    private final SpuService spuService;

    public SpuController(SpuService spuService) {
        this.spuService = spuService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:spu:page")
    public Result<PageData<SpuDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<SpuDTO> page = spuService.page(params);

        return new Result<PageData<SpuDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:spu:info")
    public Result<SpuDTO> get(@PathVariable("id") Long id) {
        SpuDTO data = spuService.get(id);

        return new Result<SpuDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:spu:save")
    public Result save(@RequestBody SpuDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        spuService.save(dto);
        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:spu:update")
    public Result update(@RequestBody SpuDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        spuService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:spu:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        spuService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @LogOperation("器材信息导出")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SpuDTO> list = spuService.list(params);
        String currentDate = DateUtils.format(new Date());
        String fileName = "器材信息导出_" + currentDate;
        ExcelUtils.exportExcelToTarget(response, fileName, list, SpuExcel.class);
    }
}